{include file="public/header296" /}
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-form layui-card-header layuiadmin-card-header-auto" style="padding-top: 10px;">
      <!-- 搜索 -->
      <div class="layui-form-item">
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input type="text" name="article_id" placeholder="请输入文章ID" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input type="text" name="keywords" placeholder="请输入搜索关键字" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <input type="text" name="editor" placeholder="请输入作者" autocomplete="off" class="layui-input">
          </div>
        </div>
        <div class="layui-inline">
          <div class="layui-input-inline">
            <select name="category_id" id="category_id">
              <option value="">请选择分类</option>
              {notempty name="category_list"}
                {volist name="category_list" id="vo"}
                  <option value="{$vo.id}">{$vo.name}</option>
                {/volist}
              {/notempty}
            </select>
          </div>
        </div>
        <div class="layui-inline">
          <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="LAY-app-content-search">
            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
          </button>
        </div>
      </div>
    </div>
    <div class="layui-card-body">
      <table class="layui-hide" id="LAY-app-content-list" lay-filter="LAY-app-content-list"></table>
      <script type="text/html" id="toolbarMenu">
        <button class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="batchdel">删除</button>
        <button class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="insert">添加</button>
        <button class="layui-btn layui-btn-sm layuiadmin-btn-list" data-type="category">分类管理</button>
      </script>
      <script type="text/html" id="buttonTpl">
        {{#  if(d.status == 1){ }}
        <button class="layui-btn layui-btn-xs">已发布</button>
        {{# } else if(d.status === 2) { }}
        <button class="layui-btn layui-btn-danger layui-btn-xs">系统下架</button>
        {{#  } else { }}
        <button class="layui-btn layui-btn-primary layui-btn-xs">暂不发布</button>
        {{#  } }}
      </script>
      <script type="text/html" id="table-content-list">
        {{#  if(d.status == 1){ }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i></a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="hide" title="隐藏"><i class="layui-icon layui-icon-close-fill"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" title="删除"><i class="layui-icon layui-icon-delete"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="comment" title="评论"><i class="layui-icon layui-icon-reply-fill"></i></a>
        {{# } else if(d.status == 2) { }}
        <a class="layui-btn layui-btn-primary layui-btn-xs" title="已被平台下架，禁止操作"><i class="layui-icon layui-icon-close-fill"></i></a>
        {{#  } else { }}
        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit" title="编辑"><i class="layui-icon layui-icon-edit"></i></a>
        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="pub" title="发布"><i class="layui-icon layui-icon-ok"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="delete" title="删除"><i class="layui-icon layui-icon-delete"></i></a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="comment"title="评论"><i class="layui-icon layui-icon-reply-fill"></i></a>
        {{#  } }}
      </script>
    </div>
  </div>
</div>
<script>
  layui.use(['layer', 'table', 'laydate'], function () {
    var $ = layui.$
            , table = layui.table
            , laydate = layui.laydate
            , form = layui.form;
    table.render({
      elem: '#LAY-app-content-list'
      , url: "{:url('article/getArticleList')}"
      , request: {
        pageName: 'page',
        limitName: 'page_size'
      }
      , page: {
        layout: ['limit', 'count', 'prev', 'page', 'next']
        , first: '首页'
        , last: '尾页'
        , limit: 10
      }
      , toolbar: '#toolbarMenu'
      , defaultToolbar: ['filter', 'exports', 'print', {
        title: '提示',
        layEvent: 'LAYTABLE_TIPS',
        icon: 'layui-icon-tips'
      }]
      , totalRow: true
      , cols: [[
        {type: 'checkbox' }
        , {field:'id', width:80, title: 'ID'}
        , {field: 'title', width: 200, title: '标题', align: 'center'}
        , {field: 'thumb_url', title: "缩略图", width: 160, align:'center', templet: function (d) {
          if (d.thumb_url != '') {
            return '<img layer-src="'+ d.thumb_url +'" width="160" src="' + d.thumb_url + '">';
          } else{
            return '';
          }
        }}
        , {field: 'category_name', title:'分类',align:'center', width:100}
        , {field: 'editor', title: '作者', align:'center', width:100}
        , {field: 'add_like',align:'center', title: '点赞量', width:80, sort: true}
        , {field: 'views',align:'center', title: '浏览量', width:80, sort: true}
        , {field: 'create_time', title: '创建时间', width: 150}
        , {field: 'status', title: '状态', templet: '#buttonTpl', minWidth: 45}
        , {field: 'is_rec', title: '推荐', minWidth: 40, templet: function (d) {
          if (d.is_rec) {
            return '<button class="layui-btn layui-btn-xs">推荐</button>';
          } else{
            return '<button class="layui-btn layui-btn-xs layui-btn-primary">未推荐</button>';
          }
        }}
        , {fixed: 'right', title: '操作', minWidth: 200, align: 'center', toolbar: '#table-content-list'}
      ]]
      , text: {
        none: '暂无相关数据'
      }
      , done: function (){
        // 动态监听表头高度变化，fixed行 跟着改变高度
        // $(".layui-table-header tr").resize(function () {
        //   $(".layui-table-header tr").each(function (index, val) {
        //     $($(".layui-table-fixed .layui-table-header table tr")[index]).height($(val).height());
        //   });
        // });
        // // 初始化高度，使得 fixed行表头高度一致
        // $(".layui-table-header tr").each(function (index, val) {
        //   $($(".layui-table-fixed .layui-table-header table tr")[index]).height($(val).height());
        // });
        // 动态监听表体高度变化，fixed 行跟着改变高度
        $(".layui-table-body tr").resize(function () {
          $(".layui-table-body tr").each(function (index, val) {
            $($(".layui-table-fixed .layui-table-body table tr")[index]).height($(val).height());
          });
        });
        // 初始化高度，使得 fixed 行同 tr 高度一致
        $(".layui-table-body tr").each(function (index, val) {
          $($(".layui-table-fixed .layui-table-body table tr")[index]).height($(val).height());
        });
      }
    });

    // 监听搜索
    form.on('submit(LAY-app-content-search)', function(data){
      var field = data.field;
      table.reload('LAY-app-content-list', {
        page: {
          curr: 1
        }
        , where: field
      });
    });

    // 监听工具条
    table.on('tool(LAY-app-content-list)', function(obj){
      var data = obj.data;
      var article_id = data.id;
      if(obj.event === 'delete'){
        layer.confirm('确定删除该条记录吗？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('article/delete')}",
            type:'post',
            data:{'id':article_id},
            success: function(res){
              if(res.code == 0){
                obj.del();
                layer.msg(res.msg,{icon:1});
              } else {
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if(obj.event === 'hide'){
        layer.confirm('确定隐藏该条记录吗？', {title : '温馨提示'}, function(index){
          $.ajax({
            url:"{:url('article/action')}",
            type:'post',
            data:{'id':article_id, type:0},
            success:function(res){
              if(res.code == 0){
                layer.msg(res.msg,{icon:1});
                table.reload('LAY-app-content-list');
              } else {
                layer.msg(res.msg,{icon:5});
              }
            }
          });
        });
      } else if(obj.event === 'pub'){
        $.ajax({
          url:"{:url('article/action')}",
          type:'post',
          data:{'id':article_id, type:1},
          success:function(res){
            if(res.code == 0){
              layer.msg(res.msg,{icon:1});
              table.reload('LAY-app-content-list');
            } else {
              layer.msg(res.msg,{icon:5});
            }
          }
        });
      } else if(obj.event === 'edit'){
        var index = layer.open({
          type: 2
          , title: '编辑文章'
          , content: "{:url('article/edit')}?id=" + data.id
          , maxmin: true
          , area: ['85%', '85%']
          , btn: ['确定', '取消']
          , yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-edit");

            iframeWindow.layui.form.on('submit(Lay-app-form-edit)', function(data){
              var field = data.field;
              if(field.is_rec){
                field.is_rec = 1;
              } else {
                field.is_rec = 0;
              }
              if(field.status){
                field.status = 1;
              } else {
                field.status = 0;
              }
              $.ajax({
                url:"{:url('article/edit')}",
                type:'post',
                data:{'field':field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    table.reload('LAY-app-content-list');
                    layer.close(index);
                  } else{
                    layer.msg(res.msg,{icon:5});
                  }
                }
              });
            });
            submit.trigger('click');
          }
        });
        layer.full(index);
      }
    });

    var $ = layui.$, active = {
      batchdel: function(){
        var checkStatus = table.checkStatus('LAY-app-content-list')
                ,checkData = checkStatus.data;
        var id = new Array();
        for(var i in checkData) {
          id[i] = checkData[i].id;
        }
        if(checkData.length === 0){
          layer.tips('请选择需要删除的行', this, {
            tips: [3, '#000'],
            time: 1500
          });
        } else{
          layer.confirm('确定删除吗？', function(index) {
            $.ajax({
              url:"{:url('article/delete')}",
              type:'post',
              data:{'id':id},
              success:function(res){
                if(res.code == 0){
                  layer.msg(res.msg,{icon:1});
                  table.reload('LAY-app-content-list');
                } else {
                  layer.msg(res.msg,{icon:5});
                }
              }
            });
          });
        }
      },
      insert: function(){
        var index = layer.open({
          type: 2
          ,title: '添加文章'
          ,content: "{:url('article/insert')}"
          ,maxmin: true
          ,area: ['85%', '85%']
          ,btn: ['确定', '取消']
          ,yes: function(index, layero){
            var iframeWindow = window['layui-layer-iframe'+ index]
                    ,submit = layero.find('iframe').contents().find("#Lay-app-form-submit");

            iframeWindow.layui.form.on('submit(Lay-app-form-submit)', function(data){
              var field = data.field;
              if(field.is_rec){
                field.is_rec = 1;
              } else {
                field.is_rec = 0;
              }
              if(field.status){
                field.status = 1;
              } else {
                field.status = 0;
              }
              if(field.title == ''){ layer.msg('模板不能为空', {icon:5});}
              $.ajax({
                url:"{:url('article/insert')}",
                type:'post',
                data:{'field': field},
                success:function(res){
                  if(res.code == 0){
                    layer.msg(res.msg,{icon:1});
                    table.reload('LAY-app-content-list');
                    layer.close(index);
                  } else{
                    return layer.msg(res.msg,{icon:5});
                  }
                }
              });

            });
            submit.trigger('click');
          }
        });
        layer.full(index);
      },
      category: function(){
        window.location.href = "{:url('article/category')}";
      }
    };

    // 监听删除，添加
    $('.layui-btn.layuiadmin-btn-list').on('click', function(){
      var type = $(this).data('type');
      active[type] ? active[type].call(this) : '';
    });
  });
</script>
{include file="public/footer" /}
